Conditions | 18 |
Paths | 73 |
Total Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like formManager.getFormFields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var formManager = function(){ |
||
92 | getFormFields: function (form, isSubmission) { |
||
93 | var usernameField = null; |
||
94 | |||
95 | // Locate the password field(s) in the form. Up to 3 supported. |
||
96 | // If there's no password field, there's nothing for us to do. |
||
97 | var pwFields = this._getPasswordFields(form, isSubmission); |
||
98 | if (!pwFields){ |
||
99 | return [null, null, null]; |
||
100 | } |
||
101 | |||
102 | |||
103 | |||
104 | // Locate the username field in the form by searching backwards |
||
105 | // from the first passwordfield, assume the first text field is the |
||
106 | // username. We might not find a username field if the user is |
||
107 | // already logged in to the site. |
||
108 | for (var i = pwFields[0].index - 1; i >= 0; i--) { |
||
109 | if(!this.isElementVisible(form.elements[i])){ |
||
110 | continue; |
||
111 | } |
||
112 | if (form.elements[i].type.toLowerCase() === "text" || form.elements[i].type.toLowerCase() === "email") { |
||
113 | usernameField = form.elements[i]; |
||
114 | break; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | if (!usernameField){ |
||
119 | this.log('(form ('+ form.action +') ignored -- no username field found)'); |
||
120 | } |
||
121 | |||
122 | |||
123 | // If we're not submitting a form (it's a page load), there are no |
||
124 | // password field values for us to use for identifying fields. So, |
||
125 | // just assume the first password field is the one to be filled in. |
||
126 | if (!isSubmission || pwFields.length === 1){ |
||
127 | var res = [usernameField, pwFields[0].element]; |
||
128 | if(pwFields[1]){ |
||
129 | res.push(pwFields[1].element); |
||
130 | } else { |
||
131 | res.push(null); |
||
132 | } |
||
133 | return res; |
||
134 | } |
||
135 | |||
136 | |||
137 | |||
138 | // Try to figure out WTF is in the form based on the password values. |
||
139 | var oldPasswordField, newPasswordField; |
||
140 | var pw1 = pwFields[0].element.value; |
||
141 | var pw2 = pwFields[1].element.value; |
||
142 | var pw3 = (pwFields[2] ? pwFields[2].element.value : null); |
||
143 | |||
144 | if (pwFields.length === 3) { |
||
145 | // Look for two identical passwords, that's the new password |
||
146 | |||
147 | if (pw1 === pw2 && pw2 === pw3) { |
||
148 | // All 3 passwords the same? Weird! Treat as if 1 pw field. |
||
149 | newPasswordField = pwFields[0].element; |
||
150 | oldPasswordField = null; |
||
151 | } else if (pw1 === pw2) { |
||
152 | newPasswordField = pwFields[0].element; |
||
153 | oldPasswordField = pwFields[2].element; |
||
154 | } else if (pw2 === pw3) { |
||
155 | oldPasswordField = pwFields[0].element; |
||
156 | newPasswordField = pwFields[2].element; |
||
157 | } else if (pw1 === pw3) { |
||
158 | // A bit odd, but could make sense with the right page layout. |
||
159 | newPasswordField = pwFields[0].element; |
||
160 | oldPasswordField = pwFields[1].element; |
||
161 | } else { |
||
162 | // We can't tell which of the 3 passwords should be saved. |
||
163 | this.log("(form ignored -- all 3 pw fields differ)"); |
||
164 | return [null, null, null]; |
||
165 | } |
||
166 | } else { // pwFields.length == 2 |
||
167 | if (pw1 === pw2) { |
||
168 | // Treat as if 1 pw field |
||
169 | newPasswordField = pwFields[0].element; |
||
170 | oldPasswordField = null; |
||
171 | } else { |
||
172 | // Just assume that the 2nd password is the new password |
||
173 | oldPasswordField = pwFields[0].element; |
||
174 | newPasswordField = pwFields[1].element; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return [usernameField, newPasswordField, oldPasswordField]; |
||
179 | }, |
||
180 | log: function (str) { |
||
255 | formManager._init_(); |